﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ADECMS.UserPages
{
    public partial class AddNews : System.Web.UI.Page
    {
        ADECMSDBDataContext cmsdb;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                cmsdb = new ADECMSDBDataContext();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
            if (!Page.IsPostBack)
            {

                HttpCookie loginCookie = Request.Cookies["AuthCookie"];
                if (Request.IsAuthenticated)
                {
                    FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(loginCookie.Value);
                    Label5.Text = "Logovani ste " + ticket.Name.ToString();

                    var useri = from k in cmsdb.USERs
                                from p in cmsdb.USER_TYPEs
                                where k.username == ticket.Name && k.user_type_id == p.id && p.type == "Administrator"
                                select k;
                    try
                    {
                        USER kor = useri.First();
                        MenuItem apanel = new MenuItem("Admin panel");
                        Menu1.Items.Add(apanel);
                    }
                    catch (System.InvalidOperationException)
                    {
                        return;
                    }
                }
            }
        }

        protected void ButtonBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("/UserPages/Start.aspx");
        }

        protected void Submit_Click(object sender, EventArgs e)
        {
            if (ClanakNaslov.Text == "")
            {
                Label3.Text = "Morate unijeti naslov novosti";
                return;
            }
            if (Editor1.Content == "")
            {
                Label3.Text = "Morate unijeti tekst novosti";
                return;
            }
            try
            {
                NEW nov = cmsdb.NEWs.Single(n => n.title == ClanakNaslov.Text);
                Label3.Text = "Novost sa takvim naslovom već postoji";
            }
            catch (InvalidOperationException)
            {

                try
                {
                    //selektuj odgovarajucu kategoriju
                    CATEGORY kategorija = cmsdb.CATEGORies.Single(k => k.name == DropDownList1.SelectedItem.ToString());

                    //pokupi username korisnika
                    HttpCookie loginCookie = Request.Cookies["AuthCookie"];
                    FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(loginCookie.Value);
                    USER korisnik = cmsdb.USERs.Single(k => k.username == ticket.Name.ToString());

                    //napravi novost
                    NEW nova = new NEW();
                    nova.title = ClanakNaslov.Text;
                    nova.text = Editor1.Content;
                    nova.news_date = DateTime.Now;

                    //dodaj novost u bazu
                    kategorija.NEWs.Add(nova);
                    korisnik.NEWs.Add(nova);
                    cmsdb.NEWs.InsertOnSubmit(nova);
                    cmsdb.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }

                Response.Redirect("/UserPages/Start.aspx");
            }
        }

        protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
        {
            switch (e.Item.Text)
            {
                case "Sign out":
                    FormsAuthentication.SignOut();
                    Response.Redirect("/Default.aspx");
                    break;
                case "Dodaj novost":
                    Response.Redirect("/UserPages/AddNews.aspx");
                    break;
                case "Pretraga":
                    Response.Redirect("/UserPages/Search.aspx");
                    break;
                case "Admin panel":
                    Response.Redirect("/UserPages/AdminPanel.aspx");
                    break;
                case "File upload":
                    Response.Redirect("/UserPages/FileUpload.aspx");
                    break;
                case "File download":
                    Response.Redirect("/UserPages/FileDownload.aspx");
                    break;
                case "Upload prezentacije":
                    Response.Redirect("/UserPages/PresentationUpload.aspx");
                    break;
                case "Pregled prezentacije":
                    Response.Redirect("/UserPages/PresentationView.aspx");
                    break;
                case "Pregledaj profil":
                    Response.Redirect("/UserPages/ProfileView.aspx");
                    break;
                case "Edituj profil":
                    Response.Redirect("/UserPages/ProfileEdit.aspx");
                    break;
            }
        }
    }
}
